
package com.hd.sup.modules.poi.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.common.constant.BizConstant;
import com.hd.common.page.BaseController;
import com.hd.common.reqres.request.RequestData;
import com.hd.common.utils.DateUtil;
import com.hd.common.utils.ExcelStyleUtil;
import com.hd.common.utils.ValueUtils;
import com.hd.sup.modules.alarm.model.AlarmDailyStatis;
import com.hd.sup.modules.alarm.model.AlarmDetail;
import com.hd.sup.modules.alarm.service.IAlarmDailyStatisService;
import com.hd.sup.modules.alarm.service.IAlarmDetailService;
import com.hd.sup.modules.alarm.wrapper.AlarmWrapper;
import com.hd.sup.modules.poi.model.*;
import com.hd.sup.modules.terminal.model.Terminal;
import com.hd.sup.modules.terminal.service.ITerminalService;
import com.hd.sup.modules.vehicle.model.*;
import com.hd.sup.modules.vehicle.service.*;
import com.hd.sup.modules.vehicle.wrapper.VehicleTrailWrapper;
import com.hd.sup.modules.vehicle.wrapper.VehicleWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title RegionController.java
 * @Description 省市县区域控制器
 * @Package com.hd.sup.modules.district.controller
 * @date 2020/12/29 12:32 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/poi")
public class PoiController extends BaseController {

    /**
     * @Description 文件后缀
     * @Author chentl
     * @Create: 2021/2/23 10:41 上午
     */
    private static final String FILE_SUFFIX = ".xlsx";
    public static final int EXPORT_LIMIT = 20000;

    @Resource
    private ITerminalService terminalService;

    @Resource
    private IVehicleAlarmDailyStatisService vehicleAlarmDailyStatisService;

    @Resource
    private IVehicleService vehicleService;

    @Resource
    private IAlarmDailyStatisService alarmDailyStatisService;

    @Resource
    private IVehicleOnOffService vehicleOnOffService;

    @Resource
    private IVehicleStatusService vehicleStatusService;

    @Resource
    private IAlarmDetailService alarmDetailService;

    @Resource
    private IVehicleTrailService vehicleTrailService;

    @Resource
    private IVehicleEndPosService vehicleEndPosService;

    /**
     * @return void
     * @author chentl
     * @description 车辆末次位置导出
     * @date 2021/2/24 2:04 下午
     * @params [requestData, response]
     */
    @PostMapping("/vehicleEndPosExport")
    public void vehicleEndPosExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String platNo = requestData.getString("platNo");
        String platNoColor = requestData.getString("platNoColor");
        String enterpriseNo = requestData.getString("enterpriseNo");

        //入参校验
        Page<Map<String, Object>> page = new Page<>();
        VehicleEndPos param = new VehicleEndPos(platNo, platNoColor, enterpriseNo);

        if (current == null || size == null) {
            param.setLimit(EXPORT_LIMIT);
            page = null;
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleTrailWrapper(vehicleEndPosService.getVehicleEndPosPage(page, param)).warp();
            List<VehicleEndPosExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, VehicleEndPosExport.class, true)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val())).collect(Collectors.toList());


            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("历史回放轨迹明细信息");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, VehicleEndPosExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "历史回放轨迹明细信息" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }


    /**
     * @return void
     * @author chentl
     * @description 轨迹点导出
     * @date 2021/2/24 1:39 下午
     * @params [requestData, response]
     */
    @PostMapping("/gpsPointsExport")
    public void gpsPointsExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String platNo = requestData.getString("platNo");
        String platNoColor = requestData.getString("platNoColor");
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        if (StringUtils.isAnyEmpty(platNo, platNoColor, startDateStr, endDateStr)) return;

        VehicleTrail param = new VehicleTrail(platNo, platNoColor, startDateStr, endDateStr);
        Page<Map<String, Object>> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleTrailWrapper(vehicleTrailService.getVehicleTrackPage(page, param)).warp();
            List<GpsPointsExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, GpsPointsExport.class, true)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val())).collect(Collectors.toList());

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("历史回放轨迹明细信息");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, GpsPointsExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "历史回放轨迹明细信息" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }


    }


    /**
     * @return void
     * @author chentl
     * @description 报警明细导出
     * @date 2021/2/24 12:04 下午
     * @params [requestData, response]
     */
    @PostMapping("/alarmDetailExport")
    public void alarmDetailExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        String alarmType = requestData.getString("alarmType");
        String platNo = requestData.getString("platNo");
        String regionCode = requestData.getString("regionCode");
        String verifyStatus = requestData.getString("verifyStatus");
        String handleStatus = requestData.getString("handleStatus");
        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        AlarmDetail param = new AlarmDetail(platNo, alarmType, DateUtil.fillDate(startDateStr, "start"), DateUtil.fillDate(endDateStr, "end"), dateRange, enterpriseNo, enterpriseName, regionCode, handleStatus, verifyStatus, sort, order);
        Page<Map<String, Object>> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = (List<Map<String, Object>>) new AlarmWrapper(alarmDetailService.getAlarmPage(page, param)).warp();
            List<AlarmDetailExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, AlarmDetailExport.class, true)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> {
                    one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val());
                    one.setAlarm_type(BizConstant.AlarmType.get(one.getAlarm_type()).val());
                }).collect(Collectors.toList());


            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("报警明细信息");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, AlarmDetailExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "报警明细信息" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }


    /**
     * @return void
     * @author chentl
     * @description 离线车辆导出
     * @date 2021/2/24 11:25 上午
     * @params [requestData, response]
     */
    @PostMapping("/vehicleOffLineExport")
    public void vehicleOffLineExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String platNo = requestData.getString("platNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        String regionCode = requestData.getString("regionCode");
        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        Integer onlineStatus = ValueUtils.parseInteger(requestData.getString("onlineStatus"));
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        VehicleOnOff param = new VehicleOnOff(DateUtil.fillDate(startDateStr, "start"), DateUtil.fillDate(endDateStr, "end"), dateRange, enterpriseNo, platNo, enterpriseName, regionCode, onlineStatus, sort, order);
        Page<Map<String, Object>> page = new Page<>();
        if (current == null || size == null) {
            page = null;
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleWrapper(vehicleStatusService.getOffLineVehicleList(page, param)).warp();
            List<VehicleOffLineExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, VehicleOffLineExport.class, true)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val())).collect(Collectors.toList());

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("离线车辆");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, VehicleOffLineExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "离线车辆" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }

    /**
     * @return void
     * @author chentl
     * @description 车辆行驶导出
     * @date 2021/2/24 11:24 上午
     * @params [requestData, response]
     */
    @PostMapping("/vehicleDrivingExport")
    public void vehicleDrivingExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String platNo = requestData.getString("platNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        String regionCode = requestData.getString("regionCode");
        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        Integer onlineStatus = ValueUtils.parseInteger(requestData.getString("onlineStatus"));
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        VehicleOnOff param = new VehicleOnOff(DateUtil.fillDate(startDateStr, "start"), DateUtil.fillDate(endDateStr, "end"), dateRange, enterpriseNo, platNo, enterpriseName, regionCode, onlineStatus, sort, order);
        Page<Map<String, Object>> page = new Page<>();
        if (current == null || size == null) {
            page = null;
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleWrapper(vehicleOnOffService.getVehicleDrivingPage(page, param)).warp();
            List<VehicleDrivingExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, VehicleDrivingExport.class, true)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val())).collect(Collectors.toList());


            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("车辆行驶统计");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, VehicleDrivingExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "车辆行驶统计" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }

    /**
     * @return void
     * @author chentl
     * @description 企业处置率导出
     * @date 2021/2/24 11:24 上午
     * @params [requestData, response]
     */
    @PostMapping("/enterpriseHandleRateAlarmExport")
    public void enterpriseHandleRateAlarmExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange);

        Page<Map<String, Object>> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }

        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = alarmDailyStatisService.getEnterpriseAlarmHanderRatePage(page, param);
            List<EnterpriseAlarmExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, EnterpriseAlarmExport.class, true)).collect(Collectors.toList());

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("企业报警处理统计");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, EnterpriseAlarmExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "企业报警处理统计" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }


    /**
     * @return void
     * @author chentl
     * @description 区域处置率导出
     * @date 2021/2/24 11:24 上午
     * @params [requestData, response]
     */
    @PostMapping("/regionHandleRateAlarmExport")
    public void regionHandleRateAlarmExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange);

        Page<Map<String, Object>> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }

        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = alarmDailyStatisService.getRegionAlarmHanderRatePage(page, param);
            List<RegionHandleRateAlarmExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, RegionHandleRateAlarmExport.class, true)).collect(Collectors.toList());

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("地区报警处理统计");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, RegionHandleRateAlarmExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "地区报警处理统计" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }


    }

    /**
     * @return void
     * @author chentl
     * @description 企业报警统计表格导出
     * @date 2021/2/23 10:40 上午
     * @params [requestData, response]
     */
    @PostMapping("/enterpriseAlarmExport")
    public void enterpriseAlarmExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();

        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");

        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange, sort, order);
        Page<AlarmDailyStatis> page = new Page<>();

        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }

        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<EnterpriseAlarmExport> apiList = alarmDailyStatisService.getAlarmEnterpriseExport(page, param);

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("企业报警统计");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, EnterpriseAlarmExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "企业报警统计" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }

    /**
     * @return void
     * @author chentl
     * @description 区域报警统计表格导出
     * @date 2021/2/23 10:40 上午
     * @params [requestData, response]
     */
    @PostMapping("/regionAlarmExport")
    public void regionAlarmExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();

        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");

        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        AlarmDailyStatis param = new AlarmDailyStatis(enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange, sort, order);

        Page<AlarmDailyStatis> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }

        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<RegionAlarmExport> apiList = alarmDailyStatisService.getAlarmRegionExport(page, param);

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("区域报警统计");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, RegionAlarmExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "区域报警统计" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }

    /**
     * @return void
     * @author chentl
     * @description 车辆报警统计表格导出
     * @date 2021/2/23 10:40 上午
     * @params [requestData, response]
     */
    @PostMapping("/vehicleAlarmExport")
    public void vehicleAlarmExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();

        String startDateStr = requestData.getString("startDateStr");
        String endDateStr = requestData.getString("endDateStr");
        String regionCode = requestData.getString("regionCode");
        String enterpriseNo = requestData.getString("enterpriseNo");
        String platNo = requestData.getString("platNo");
        String enterpriseName = requestData.getString("enterpriseName");
        String dateRange = requestData.getString("dateRange");
        String sort = requestData.getString("sort");
        String order = requestData.getString("order");
        //入参校验
        if (StringUtils.isNotBlank(dateRange) && (StringUtils.isNotBlank(startDateStr) || StringUtils.isNotBlank(endDateStr)))
            return;

        VehicleAlarmDailyStatis param = new VehicleAlarmDailyStatis(platNo, enterpriseNo, enterpriseName, regionCode, startDateStr, endDateStr, dateRange, sort, order);

        Page<VehicleAlarmDailyStatis> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }

        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<VehicleAlarmExport> apiList = vehicleAlarmDailyStatisService.getAlarmVehicleExportData(page, param);
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val())).collect(Collectors.toList());

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("车辆报警统计");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, VehicleAlarmExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "车辆报警统计" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }
    }

    /**
     * @return void
     * @author chentl
     * @description 车辆静态信息导出
     * @date 2021/2/24 2:16 下午
     * @params [requestData, response]
     */
    @PostMapping("/vehicleExport")
    public void vehicleExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String platNo = requestData.getString("platNo");
        String vehicleType = requestData.getString("vehicleType");
        String transType = requestData.getString("transType");
        String regionCode = requestData.getString("regionCode");
        //入参校验
        Vehicle param = new Vehicle(platNo, vehicleType, transType, regionCode);

        Page<VehicleAlarmDailyStatis> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = (List<Map<String, Object>>) new VehicleWrapper(vehicleService.pageList(page, param)).warp();
            List<VehicleExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, VehicleExport.class, true)).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(apiList))
                apiList.stream().peek(one -> one.setPlat_no_color(BizConstant.VehicleNoColor.get(one.getPlat_no_color()).val())).collect(Collectors.toList());


            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("车辆静态信息");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, VehicleExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "车辆静态信息" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }
    }

    /**
     * @return void
     * @author chentl
     * @description 终端静态信息导出
     * @date 2021/2/24 2:17 下午
     * @params [requestData, response]
     */
    @PostMapping("/terminalExport")
    public void terminalExport(@RequestBody RequestData requestData, HttpServletResponse response) throws IOException {
        Long current = requestData.getCurrent();
        Long size = requestData.getSize();
        String terminalNo = requestData.getString("terminalNo");
        String terminalType = requestData.getString("terminalType");
        String installDateStr = requestData.getString("installDateStr");
        //入参校验
        Terminal param = new Terminal(terminalNo, terminalType, installDateStr);

        Page<VehicleAlarmDailyStatis> page = new Page<>();
        if (current == null || size == null) {
            page = null;
            param.setLimit(EXPORT_LIMIT);
        } else {
            page.setCurrent(current);
            page.setSize(size);
        }


        ServletOutputStream servletOutputStream = null;
        Workbook workbook = null;
        try {
            List<Map<String, Object>> result = terminalService.pageList(page, param);
            List<TerminalExport> apiList = result.stream().map(one -> BeanUtil.mapToBean(one, TerminalExport.class, true)).collect(Collectors.toList());

            //生成excel文件
            ExportParams params = new ExportParams();
            params.setSheetName("终端静态信息");
            params.setType(ExcelType.XSSF);
            params.setStyle(ExcelStyleUtil.class);
            workbook = ExcelExportUtil.exportExcel(params, TerminalExport.class, apiList);


            String date = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            String filename = "终端静态信息" + date + FILE_SUFFIX;
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            response.addHeader("X-Frame-Options", "SAMEORIGIN");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
            //返回文件流
            servletOutputStream = response.getOutputStream();
            workbook.write(servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (servletOutputStream != null) {
                servletOutputStream.close();
                servletOutputStream = null;
            }
            if (workbook != null) {
                workbook.close();
                workbook = null;
            }
        }

    }


}
